<?php
class CategoriesController extends AppController {

    var $name = 'Categories';
    var $helpers = array('Javascript');
    var $components = array('Json');

    function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('index');
    }
    
     /**
      * Creating new tag
      * usage in : /posts/add.ctp
      */
    function add(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $categories = Sanitize::escape($this->params['form']['category']);
        if(empty($categories)){$this->Json->error('0');}
        
        /* Checking whether category is exist or not */
        $check = $this->Category->checkCategoryExist($categories);
        if(empty($check)){
            $this->Category->create();
            $this->data['Category']['name']=$categories;
            $this->data['Category']['slug']= Inflector::slug($categories);
            $this->Category->save($this->data);            
            $this->Json->show(array('result'=>'1'));
        }else{
            $this->Json->error('1');
        }
    }

    /**
     * Showing category list
     */
    function category(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        $categories = $this->Category->categoryList();
        $this->Json->show(array('result'=>'1','category'=>$categories));
    }

}
?>
